package javaDemo06;

import java.util.Scanner;

public class JingZiQi {
    public static void main(String[] args) {
        String[][] arr = make();
        boolean f = true;
        while (f) {
            robot(arr);
            show(arr);
            f = winner(arr);
            input(arr);
            f = winner(arr);
        }
    }

    public static String[][] make() {
        String[][] arr = new String[3][3];
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                arr[i][j] = "*\t";
            }
        }
        return arr;
    }

    public static void robot(String[][] arr) {
        boolean f = true;
        System.out.println("机器人下在了这里");
        while (f) {
            int a = (int) (Math.random() * 3);
            int b = (int) (Math.random() * 3);
            if (arr[a][b].equals("X\t") || arr[a][b].equals("O\t")) {
                continue;
            } else {
                arr[a][b] = "O\t";
                f = false;
            }
        }
    }

    public static void input(String[][] arr) {
        Scanner sc = new Scanner(System.in);
        boolean f = false;
        while (!f) {
            System.out.println("请输入要填入X的行数，范围1至3");
            int a = sc.nextInt() - 1;
            if (a > 2 || a < 0) {
                System.out.println("输入有误请重新输入");
                continue;
            }
            System.out.println("请输入要填入X的列数，范围1至3");
            int b = sc.nextInt() - 1;
            if (b < 0 || b > 2) {
                System.out.println("输入有误请重新输入");
                continue;
            }
            if (arr[a][b].equals("X\t") || arr[a][b].equals("O\t")) {
                System.out.println("你要填的地方已被占用，请重新输入");
            } else {
                arr[a][b] = "X\t";
                f = true;
            }

        }


    }

    public static boolean winner(String[][] arr) {
        boolean f = true;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i][0].equals(arr[i][1]) && arr[i][0].equals(arr[i][2])) {
                if (arr[i][0].equals("X\t")) {
                    System.out.println("恭喜你赢了");
                    f = false;
                    break;
                } else if (arr[i][0].equals("O\t")) {
                    System.out.println("抱歉你输了");
                    f = false;
                    break;
                }

            }
            if (arr[0][i].equals(arr[1][i]) && arr[1][i].equals(arr[2][i])) {
                if (arr[1][i].equals("X\t")) {
                    System.out.println("恭喜你赢了");
                    f = false;
                    break;
                } else if (arr[1][i].equals("O\t")) {
                    System.out.println("抱歉你输了");
                    f = false;
                    break;
                }

            }

        }
        if (arr[0][0].equals(arr[1][1]) && arr[1][1].equals(arr[2][2])) {

            if (arr[1][1].equals("X\t")) {
                System.out.println("恭喜你赢了");
                f = false;
            } else if (arr[1][1].equals("O\t")) {
                System.out.println("可惜你输了");
                f = false;
            }

        }
        if (arr[0][2].equals(arr[1][1]) && arr[1][1].equals(arr[2][0])) {
            if (arr[1][1].equals("X\t")) {
                System.out.println("恭喜你赢了");
                f = false;
            } else if (arr[1][1].equals("O\t")) {
                System.out.println("可惜你输了");
                f = false;
            }

        }
        return f;
    }

    public static void show(String[][] arr) {
        System.out.println("—————————————");
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {

                if (j == 2) {
                    System.out.println(arr[i][j] + "|");
                    System.out.println("—————————————");
                } else if (j == 0) {
                    System.out.print("|");
                    System.out.print(arr[i][j] + "|");
                } else {
                    System.out.print(arr[i][j] + "|");
                }
            }

        }
    }
}
